package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.StringUtils;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImpl extends ServiceImpl<ServiceItemMapper,ServiceItem>
        implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private RuntimeService runtimeService;

    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        Page<ServiceItem> page = new Page<>(qo.getPageNum(),qo.getPageSize());
        QueryWrapper<ServiceItem> wrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(qo.getKeyName())){
            wrapper.like("name",qo.getKeyName());
        }
        if (!StringUtils.isNull(qo.getCarPackage())){
            wrapper.eq("car_package",qo.getCarPackage());
        }
        if (!StringUtils.isNull(qo.getServiceCatalog())){
            wrapper.eq("service_catalog",qo.getServiceCatalog());
        };
        if (!StringUtils.isNull(qo.getAuditStatus())){
            wrapper.eq("audit_status",qo.getAuditStatus());
        }
        if (!StringUtils.isNull(qo.getSaleStatus())){
            wrapper.eq("sale_status",qo.getSaleStatus());
        }
        return new TablePageInfo<ServiceItem>(super.page(page,wrapper));
    }


   /* @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem service = serviceItemMapper.selectById(serviceItem.getId());
        if (ServiceItem.SALESTATUS_ON.equals(service.getSaleStatus())) {
            //如果是上架状态，不允许修改的,抛出异常
            throw new BusinessException("已上架，不允许修改");
        } else {
            //   套餐，如果是审核中,不能进行编辑，抛出异常
            if (ServiceItem.CARPACKAGE_YES.equals(service.getCarPackage()) &&
                    ServiceItem.AUDITSTATUS_AUDITING.equals(service.getAuditStatus())) {
                throw new BusinessException("审核中，不允许修改");
                //如果是下架状态，套餐，处于审核通过,如果此时修改，应该把状态修改成初始化
            } else if (ServiceItem.CARPACKAGE_YES.equals(service.getCarPackage())
                    && ServiceItem.AUDITSTATUS_APPROVED.equals(service.getAuditStatus())) {
                    serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
            }
        }
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_ON);
        QueryWrapper<ServiceItem> wrapper = new QueryWrapper<>();
        serviceItemMapper.update(serviceItem,wrapper);
    }*/


    @Override
    public void changeStatus(Long id) {
        UpdateWrapper<ServiceItem> wrapper=new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("sale_status",ServiceItem.SALESTATUS_OFF);
        serviceItemMapper.update(null,wrapper);
    }

    @Override
    public void changeStatusSaleOn(Long id) {
        //判断逻辑
        ServiceItem serviceItem = serviceItemMapper.selectById(id);
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())&&
                !ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("审核没通过,不能上架");
        }
        UpdateWrapper<ServiceItem> wrapper=new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("sale_status",ServiceItem.SALESTATUS_ON);
        serviceItemMapper.update(null,wrapper);
    }

    @Override
    public void startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        //处于初始化 或者 重新调整
        ServiceItem serviceItem = serviceItemMapper.selectById(id);
        if (!(ServiceItem.AUDITSTATUS_INIT.equals(serviceItem.getAuditStatus())
                || ServiceItem.AUDITSTATUS_REPLY.equals(serviceItem.getAuditStatus()))){
            throw new BusinessException("非法操作");
        }

        CarPackageAudit audit = new CarPackageAudit();
        audit.setServiceItemId(id);
        audit.setServiceItemName(serviceItem.getName());
        audit.setServiceItemInfo(serviceItem.getInfo());
        audit.setServiceItemPrice(serviceItem.getDiscountPrice());
        audit.setCreator(ShiroUtils.getUser().getUserName());
        //封装CarPackageAudit对象信息(把店长的ID设置审核人)
        audit.setAuditorId(director);
        audit.setBpmnInfoId(bpmnInfoId);
        audit.setInfo(info);
        audit.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);
        audit.setCreateTime(new Date());
        //保存CarPackageAudit对象
        carPackageAuditMapper.insert(audit);
        // 根据,bpmnInfoID查询bpmnInfo对象
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectById(bpmnInfoId);
        //流程变量(map): 折扣金额,店长ID,财务ID
        Map<String,Object> map=new HashMap<>();
        if (director!=null){
            map.put("director",director);
        }
        if (finance!=null){
            map.put("finance",finance);
        }
        map.put("discountPrice",serviceItem.getDiscountPrice().longValue());
        //根据bpmnInfo对象的流程定义Key启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey
                (bpmnInfo.getActProcessKey(), audit.getId().toString(), map);

        //启动流程之后获取到流程实例对象
        audit.setInstanceId(processInstance.getProcessInstanceId());
        carPackageAuditMapper.updateByInstanceId(audit);
        //更新养修单项
        serviceItemMapper.changeStatus(serviceItem.getId(),ServiceItem.AUDITSTATUS_AUDITING);

    }



}
